package org.cestum.cave.ui.components
{
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import spark.primitives.Graphic;

	public class FullScreenScrollBar extends Sprite
	{
		private var _grabberPressColor:uint;
		private var _easeAmount:int;
		private var _grabber:Sprite;
		private var _trackThickness:int;
		private var whiteBoxWidth:Number = 16;
		private var whiteBoxHeight:Number = 16;
		private var _stageH:Number;
		private var _gripColor:uint;
		private var _stageW:Number;
		private var _content:DisplayObject;
		private var _grabberColor:uint;
		private var _defaultPosition:Number;
		private var _track:Sprite;
		private var _hasShine:Boolean;
		private var _grabberThickness:int;
		private var _pressed:Boolean = false;
		private var _scrollValue:Number;
		private var _cH:Number;
		private var _grabberArrow1:Sprite;
		private var _grabberArrow2:Sprite;
		private var _tH:Number;
		private var _grabberGrip:Sprite;
		private var _trackColor:uint;
		
		public function FullScreenScrollBar(param1:DisplayObject, param2:uint, param3:uint, param4:uint, param5:uint, param6:int, param7:int, param8:int, param9:Boolean)
		{
			_content = param1;
			_trackColor = param2;
			_grabberColor = param3;
			_grabberPressColor = param4;
			_gripColor = param5;
			_trackThickness = param6;
			_grabberThickness = param7;
			_easeAmount = param8;
			_hasShine = param9;
			init();
			return;
		}// end function
		
		private function scrollContent(event:Event) : void
		{
			var _loc_2:Number = NaN;
			var _loc_3:Number = NaN;
			var _loc_4:Number = NaN;
			_loc_2 = -(_cH - _tH) * (_grabber.y / _scrollValue);
			_loc_3 = _loc_2 - _content.y + _defaultPosition;
			_loc_4 = _loc_3 / _easeAmount;
			_content.y = _content.y + Math.round(_loc_4);
			if (Math.abs(_content.y - _loc_2 - _defaultPosition) < 1.5)
			{
				_grabber.removeEventListener(Event.ENTER_FRAME, scrollContent);
				_content.y = Math.round(_loc_2) + _defaultPosition;
			}
			positionGrips();
			return;
		}// end function
		
		private function mouseWheelListener(event:MouseEvent) : void
		{
			var _loc_2:* = event.delta;
			if (_loc_2 > 0)
			{
				if (_grabber.y - _loc_2 * 4 >= 0)
				{
					_grabber.y = _grabber.y - _loc_2 * 4;
				}
				else
				{
					_grabber.y = 0;
				}
				if (!_grabber.willTrigger(Event.ENTER_FRAME))
				{
					_grabber.addEventListener(Event.ENTER_FRAME, scrollContent);
				}
			}
			else
			{
				if (_grabber.y + _grabber.height + Math.abs(_loc_2) * 4 <= stage.stageHeight - whiteBoxHeight)
				{
					_grabber.y = _grabber.y + Math.abs(_loc_2) * 4;
				}
				else
				{
					_grabber.y = stage.stageHeight - _grabber.height - whiteBoxHeight;
				}
				if (!_grabber.willTrigger(Event.ENTER_FRAME))
				{
					_grabber.addEventListener(Event.ENTER_FRAME, scrollContent);
				}
			}
			return;
		}// end function
		
		private function createGrabberGrip() : Sprite
		{
			var _loc_1:int = 7;
			var _loc_2:* = (_grabberThickness - _loc_1) / 2;
			var _loc_3:* = new Sprite();
			_loc_3.graphics.beginFill(_gripColor, 1);
			_loc_3.graphics.drawRect(_loc_2, 0, _loc_1, 1);
			_loc_3.graphics.drawRect(_loc_2, 0 + 2, _loc_1, 1);
			_loc_3.graphics.drawRect(_loc_2, 0 + 4, _loc_1, 1);
			_loc_3.graphics.drawRect(_loc_2, 0 + 6, _loc_1, 1);
			_loc_3.graphics.drawRect(_loc_2, 0 + 8, _loc_1, 1);
			_loc_3.graphics.endFill();
			return _loc_3;
		}// end function
		
		private function init() : void
		{
			createTrack();
			createGrabber();
			createGrips();
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage, false, 0, true);
			_defaultPosition = Math.round(_content.y);
			_grabber.y = 0;
			return;
		}// end function
		
		private function onAddedToStage(event:Event) : void
		{
			stage.addEventListener(Event.MOUSE_LEAVE, stopScroll);
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelListener);
			stage.addEventListener(Event.RESIZE, onStageResize, false, 0, true);
			stage.addEventListener(MouseEvent.MOUSE_UP, onUpListener, false, 0, true);
			_grabber.addEventListener(MouseEvent.MOUSE_DOWN, onDownListener, false, 0, true);
			_grabber.buttonMode = true;
			_track.addEventListener(MouseEvent.CLICK, onTrackClick, false, 0, true);
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			_stageW = stage.stageWidth;
			_stageH = stage.stageHeight;
			adjustSize();
			return;
		}// end function
		
		private function createGrabber() : void
		{
			var _loc_2:Sprite = null;
			var _loc_3:Graphics = null;
			_grabber = new Sprite();
			var _loc_1:* = new Sprite();
			_loc_1.graphics.beginFill(_grabberColor);
			_loc_1.graphics.drawRect(0, 0, _grabberThickness, _grabberThickness);
			_loc_1.graphics.endFill();
			_loc_1.name = "bg";
			_grabber.addChild(_loc_1);
			if (_hasShine)
			{
				_loc_2 = new Sprite();
				_loc_3 = _loc_2.graphics;
				_loc_3.beginFill(16777215, 0.15);
				_loc_3.drawRect(0, 0, Math.ceil(_trackThickness / 2), _trackThickness);
				_loc_3.endFill();
				_loc_2.x = Math.floor(_trackThickness / 2);
				_loc_2.name = "shine";
				_grabber.addChild(_loc_2);
			}
			addChild(_grabber);
			return;
		}// end function
		
		private function createTrack() : void
		{
			_track = new Sprite();
			var _loc_1:* = new Sprite();
			_loc_1.graphics.beginFill(_trackColor, 0.1);
			_loc_1.graphics.drawRect(0, 0, _trackThickness, _trackThickness);
			_loc_1.graphics.endFill();
			_track.addChild(_loc_1);
			addChild(_track);
			return;
		}// end function
		
		private function onUpListener(event:MouseEvent = null) : void
		{
			if (_pressed)
			{
				_pressed = false;
				_grabber.stopDrag();
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveListener);
				TweenUtil.to(_grabber.getChildByName("bg"), 0.5, {tint:null});
			}
			return;
		}// end function
		
		public function adjustSize() : void
		{
			this.x = _stageW - _trackThickness;
			_track.height = _stageH - whiteBoxHeight;
			_track.y = 0;
			_tH = _track.height;
			_cH = _content.height + _defaultPosition;
			_grabber.getChildByName("bg").height = Math.ceil(_tH / _cH * _tH);
			if (_grabber.getChildByName("bg").height < 35)
			{
				_grabber.getChildByName("bg").height = 35;
			}
			if (_hasShine)
			{
				_grabber.getChildByName("shine").height = _grabber.getChildByName("bg").height;
			}
			if (_grabber.y + _grabber.getChildByName("bg").height > _tH)
			{
				_grabber.y = _tH - _grabber.getChildByName("bg").height;
			}
			_grabber.y = _cH < _tH ? (0) : (_grabber.y);
			this.visible = _cH + 8 > _tH;
			_scrollValue = _tH - _grabber.getChildByName("bg").height;
			_content.y = Math.round(-(_cH - _tH) * (_grabber.y / _scrollValue) + _defaultPosition);
			positionGrips();
			trace(_grabber.y + " " + _grabber.height + " " + _track.height);
			if (_content.height < stage.stageHeight)
			{
				stage.removeEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelListener);
			}
			else
			{
				stage.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelListener);
			}
			return;
		}// end function
		
		private function onTrackClick(event:MouseEvent) : void
		{
			var p:int;
			var reset:Function;
			var e:* = event;
			reset = function () : void
			{
				if (_grabber.y < 0)
				{
					_grabber.y = 0;
				}
				if (_grabber.y + _grabber.getChildByName("bg").height > _track.height - whiteBoxHeight)
				{
					_grabber.y = stage.stageHeight - _grabber.getChildByName("bg").height - whiteBoxHeight;
				}
				return;
			}// end function
				;
			var s:int;
			p = Math.ceil(e.stageY);
			if (p < _grabber.y)
			{
				if (_grabber.y < _grabber.height)
				{
					TweenUtil.to(_grabber, 0.5, {y:0, onComplete:reset, overwrite:1});
				}
				else
				{
					TweenUtil.to(_grabber, 0.5, {y:"-150", onComplete:reset});
				}
				if (_grabber.y < 0)
				{
					_grabber.y = 0;
				}
			}
			else
			{
				if (_grabber.y + _grabber.height > _stageH - whiteBoxHeight)
				{
					TweenUtil.to(_grabber, 0.5, {y:_stageH - _grabber.height - whiteBoxHeight, onComplete:reset, overwrite:1});
				}
				else
				{
					TweenUtil.to(_grabber, 0.5, {y:"150", onComplete:reset});
				}
				if (_grabber.y + _grabber.getChildByName("bg").height > _track.height - whiteBoxHeight)
				{
					_grabber.y = stage.stageHeight - _grabber.getChildByName("bg").height - whiteBoxHeight;
				}
			}
			_grabber.addEventListener(Event.ENTER_FRAME, scrollContent, false, 0, true);
			return;
		}// end function
		
		private function createGrips() : void
		{
			_grabberGrip = createGrabberGrip();
			_grabber.addChild(_grabberGrip);
			_grabberArrow1 = createPixelArrow();
			_grabber.addChild(_grabberArrow1);
			_grabberArrow2 = createPixelArrow();
			_grabber.addChild(_grabberArrow2);
			_grabberArrow1.rotation = -90;
			_grabberArrow1.x = (_grabberThickness - 7) / 2 + 1;
			_grabberArrow2.rotation = 90;
			_grabberArrow2.x = (_grabberThickness - 7) / 2 + 6;
			return;
		}// end function
		
		private function positionGrips() : void
		{
			_grabberGrip.y = Math.ceil(_grabber.getChildByName("bg").y + _grabber.getChildByName("bg").height / 2 - _grabberGrip.height / 2);
			_grabberArrow1.y = _grabber.getChildByName("bg").y + 8;
			_grabberArrow2.y = _grabber.getChildByName("bg").height - 8;
			return;
		}// end function
		
		private function createPixelArrow() : Sprite
		{
			var _loc_1:* = new Sprite();
			_loc_1.graphics.beginFill(_gripColor, 1);
			_loc_1.graphics.drawRect(0, 0, 1, 1);
			_loc_1.graphics.drawRect(1, 1, 1, 1);
			_loc_1.graphics.drawRect(2, 2, 1, 1);
			_loc_1.graphics.drawRect(1, 3, 1, 1);
			_loc_1.graphics.drawRect(0, 4, 1, 1);
			_loc_1.graphics.endFill();
			return _loc_1;
		}// end function
		
		public function kill() : void
		{
			stage.removeEventListener(Event.RESIZE, onStageResize);
			return;
		}// end function
		
		private function stopScroll(event:Event) : void
		{
			onUpListener();
			return;
		}// end function
		
		private function onDownListener(event:MouseEvent) : void
		{
			_pressed = true;
			_grabber.startDrag(false, new Rectangle(0, 0, 0, _stageH - _grabber.getChildByName("bg").height - whiteBoxHeight));
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveListener, false, 0, true);
			TweenUtil.to(_grabber.getChildByName("bg"), 0.5, {tint:_grabberPressColor});
			return;
		}// end function
		
		private function onMouseMoveListener(event:MouseEvent) : void
		{
			event.updateAfterEvent();
			if (!_grabber.willTrigger(Event.ENTER_FRAME))
			{
				_grabber.addEventListener(Event.ENTER_FRAME, scrollContent, false, 0, true);
			}
			return;
		}// end function
		
		private function onStageResize(event:Event) : void
		{
			_stageW = stage.stageWidth;
			_stageH = stage.stageHeight;
			adjustSize();
			return;
		}// end function
		

	}
}